package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Car;
import com.zr.service.CarServiceZYH;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@Scope(value="prototype")
public class ShowCarActionZYH extends ActionSupport implements ServletResponseAware,ServletRequestAware{
	
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private int page;
	private int rows;
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Resource
	private CarServiceZYH carservice;
	
	public void showCar(){
		try {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int t =  (int) carservice.findCarCount();
		
		JSONArray  ja = carservice.getCarInfo(page,rows);
		JSONObject j = new JSONObject();
		j.put("total", t);//total储存数据长度作为分页的记录条数
		j.put("rows", ja);//rows储存json数组的数据，即表格展示的数据
		try {
			PrintWriter pw = resp.getWriter();
			pw.write(j.toString());
			pw.flush();
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req=arg0;
	}

	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp=arg0;
	}

}
